package com.imooc.service.impl;

import com.imooc.base.BaseInfoProperties;
import com.imooc.entity.mo.MessageMO;
import com.imooc.entity.pojo.Users;
import com.imooc.enums.MessageEnum;
import com.imooc.repository.MessageRepository;
import com.imooc.service.MsgService;
import com.imooc.service.UsersService;
import com.imooc.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class MsgServiceImpl extends BaseInfoProperties implements MsgService {
    @Autowired
    private MessageRepository messageRepository;
    @Autowired
    private UsersService usersService;
    @Override
    public void createMsg(String fromUserId, String toUserId, Integer msgType, Map msgContent) {
        MessageMO messageMO = new MessageMO();
        messageMO.setFromUserId(fromUserId);
        messageMO.setToUserId(toUserId);
        Users user = usersService.findUserByUserId(fromUserId);
        messageMO.setFromNickname(user.getNickname());
        messageMO.setFromFace(user.getFace());
        messageMO.setMsgContent(msgContent);
        if (msgType != null){
            messageMO.setMsgType(msgType);
        }
        messageMO.setCreateTime(new Date());
        messageRepository.save(messageMO);
    }

    @Override
    public List<MessageMO> queryList(String toUserId, Integer page, Integer pageSize) {
        Pageable pageable = PageRequest.of(page, pageSize, Sort.Direction.DESC,"CreateTime");
        List<MessageMO> messageMoList = messageRepository.findAllByToUserIdOrderByCreateTimeDesc(toUserId, pageable);
        for (MessageMO messageMO : messageMoList) {
            // 如果说类型是关注消息 , 则需要查询我之前是否关注过他
            if(messageMO.getMsgType()!=null&&messageMO.getMsgType()== MessageEnum.FOLLOW_YOU.type){
                Map map = messageMO.getMsgContent();
                if(map == null){
                    map = new HashMap();
                }
                String relationship = redis.get(REDIS_USER_AND_VLOGGER_RELATIONSHIP + ":" + messageMO.getToUserId() + ":" + messageMO.getFromUserId());
                if(StringUtils.isNotBlank(relationship)&&relationship.equalsIgnoreCase("1")){
                    map.put("isFriend",true);
                }else{
                    map.put("isFriend",false);
                }
                messageMO.setMsgContent(map);
            }
        }
        return messageMoList;
    }

    @Override
    public void unFollowVlogger(String myId, String vloggerId) {
        messageRepository.deleteAllByToUserIdAndFromUserIdAndMsgType(vloggerId,myId,MessageEnum.FOLLOW_YOU.type);
    }

    @Override
    public void unLikeThiComment(String myId, String vloggerId) {
        messageRepository.deleteAllByToUserIdAndFromUserIdAndMsgType(vloggerId,myId,MessageEnum.LIKE_COMMENT.type);
    }

    @Override
    public void unlikeThisVlog(String myId, String vloggerId) {
        log.info("unlikeThisVlog{},{}",myId,vloggerId);
        messageRepository.deleteAllByToUserIdAndFromUserIdAndMsgType(vloggerId,myId,MessageEnum.LIKE_VLOG.type);
    }

}
